#include "main.h"
#include "ModuleB/copy.h"
#include "ModuleA/function.h"

int main() {
    int max_processes = 2;  // 设置最大进程数 
    int max_tasks = 10;     // 设置最大备份任务数
    struct BackupTask tasks[max_tasks];

    int user_choice;

    do {
        printf("文件备份和同步\n");
        printf("1:文件备份\n");
        printf("2:文件同步\n");
        printf("0:退出\n");
        printf("请输入:");
        if (scanf("%d", &user_choice) != 1) {
            fprintf(stderr, "输入错误\n");
            exit(1);
        }

        if (user_choice == 1) {
        	//文件备份 
            int task_count = 0;
            task_count = getBackupTasks_1(tasks, max_tasks);
            
            // 创建共享内存区域
            int shared_mem_fd = shm_open("/completed_bytes", O_CREAT | O_RDWR, 0666);
            if (shared_mem_fd == -1) {
                perror("shm_open");
                exit(1);
            }

            ftruncate(shared_mem_fd, sizeof(long long));
            completed_bytes = mmap(NULL, sizeof(long long), PROT_READ | PROT_WRITE, MAP_SHARED, shared_mem_fd, 0);

            for (int i = 0; i < task_count; i++) {
                calculate_total_bytes(tasks[i].source);
            }

            // 创建进度条显示进程
            pid_t progress_pid = fork();
            if (progress_pid == -1) {
                perror("fork");
                exit(1);
            }

            if (progress_pid == 0) {
            // 子进程执行进度条显示任务
                progress_process();
                exit(0);
            }

            for (int i = 0; i < task_count; i++) {
                if (i >= max_processes) {
                    int status;
                    pid_t finished_pid = wait(&status);
                    if (finished_pid == -1) {
                        perror("wait");
                    	exit(1);
                	}
            	}

            	pid_t child_pid = fork();

            	if (child_pid == -1) {
                	perror("fork");
                	exit(1);
            	}

            	if (child_pid == 0) {
                	copy_directory(tasks[i].source, tasks[i].destination);
                	exit(0);
            	}
        	}

    		while (wait(NULL) > 0);
    		
    		total_bytes = 0;

            // 清理备份任务
            for (int i = 0; i < task_count; i++) {
                free((void *)tasks[i].source);
                free((void *)tasks[i].destination);
                free((void *)tasks[i].way);
                tasks[i].source = NULL;
                tasks[i].destination = NULL;
                tasks[i].way = NULL;
            }

            // 解除映射共享内存
            munmap((void *)completed_bytes, sizeof(long long));
            close(shared_mem_fd);

            // 移除共享内存对象
            shm_unlink("/completed_bytes");
        }else if (user_choice == 2) { 
        	//文件同步 
            int task_count = 0;
            task_count = getBackupTasks_2(tasks, max_tasks);
            
            // 创建共享内存区域
            int shared_mem_fd = shm_open("/completed_bytes", O_CREAT | O_RDWR, 0666);
            if (shared_mem_fd == -1) {
                perror("shm_open");
                exit(1);
            }

            ftruncate(shared_mem_fd, sizeof(long long));
            completed_bytes = mmap(NULL, sizeof(long long), PROT_READ | PROT_WRITE, MAP_SHARED, shared_mem_fd, 0);

            for (int i = 0; i < task_count; i++) {
            	if(strcmp(tasks[i].way, "1") == 0){
            		calculate_total_bytes(tasks[i].source);
				}else if(strcmp(tasks[i].way, "2") == 0){
					calculate_total_bytes(tasks[i].destination);
				}else if(strcmp(tasks[i].way, "3") == 0){
					compare_directories(tasks[i].source, tasks[i].destination);
					compare_directories(tasks[i].destination, tasks[i].source);
				}
            }

            // 创建进度条显示进程
            pid_t progress_pid = fork();
            if (progress_pid == -1) {
                perror("fork");
                exit(1);
            }

            if (progress_pid == 0) {
            // 子进程执行进度条显示任务
                progress_process();
                exit(0);
            }

            for (int i = 0; i < task_count; i++) {
                if (i >= max_processes) {
                    int status;
                    pid_t finished_pid = wait(&status);
                    if (finished_pid == -1) {
                        perror("wait");
                    	exit(1);
                	}
            	}

            	pid_t child_pid = fork();

            	if (child_pid == -1) {
                	perror("fork");
                	exit(1);
            	}

            	if (child_pid == 0) {
            		if(strcmp(tasks[i].way, "1") == 0){
            			sync_A_to_B(tasks[i].source, tasks[i].destination);
					}else if(strcmp(tasks[i].way, "2") == 0){
						sync_A_to_B(tasks[i].destination, tasks[i].source);
					}else if(strcmp(tasks[i].way, "3") == 0){
						bidirectional_sync(tasks[i].source, tasks[i].destination);
					}
                	exit(0);
            	}
        	}

    		while (wait(NULL) > 0);
    		
    		total_bytes = 0;
            
            // 清理备份任务
            for (int i = 0; i < task_count; i++) {
                free((void *)tasks[i].source);
                free((void *)tasks[i].destination);
                free((void *)tasks[i].way);
                tasks[i].source = NULL;
                tasks[i].destination = NULL;
                tasks[i].way = NULL;
            }
            
            // 解除映射共享内存
            munmap((void *)completed_bytes, sizeof(long long));
            close(shared_mem_fd);

            // 移除共享内存对象
            shm_unlink("/completed_bytes");
            
        }else if (user_choice != 0) {
            printf("无效的选择，请重新输入。\n");
        }

    } while (user_choice != 0);

    return 0;
}
